package com.example.j2eetest.controller;

import com.example.j2eetest.domain.Product;
import com.example.j2eetest.domain.Result;
import com.example.j2eetest.service.ProductService;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;

import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
@Tag(name = "产品表的四种操作", description = "Mybatis-Flex结合swagger3")
public class ProductController {

    @Resource
    private ProductService productService;

    //按照id查询
    @GetMapping("/{id}")
    @Operation(summary = "通过传入id查询product信息", description = "根据id查询")
    public Result get(@PathVariable Long id) {
        Product product = productService.getById(id);
        return Result.success(product);
    }

    //模糊查询
    @GetMapping ("/like")
    @Operation(summary = "传入字段查询", description = "根据字段模糊查询产品")
    public Result like(@RequestParam String likeName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("name",likeName);
        List<Product> list = productService.list(queryWrapper);
        return Result.success(list);
    }

    // 增加一个产品
    @Operation(summary = "添加产品", description = "增加一个产品")
    @PostMapping("/add")
    public Result add(@RequestBody Product product) {
        boolean save = productService.save(product);
        return Result.success(save);
    }

    // 根据id修改对应库存
    @Operation(summary = "通过两个pathVariable来修改指定库存信息", description = "根据ID更新库存数据")
    @PutMapping("/updatestock/{id}/{stock}")
    // update
    public Result update(@PathVariable Integer id,@PathVariable Integer stock) {
        Product product = new Product();
        product.setStock(stock);
        product.setId(id);
        boolean b = productService.updateById(product);
        return Result.success(b);
    }


}
